local filelog = require "filelog"

require "zrdice3enum"

local CardTool = {}

local function is_allsame(all_points)
    local is_all_same = false
    local same_value = 0
    if not all_points or #all_points ~= 3 then return is_all_same, same_value end
    is_all_same = true
    for index, point in pairs(all_points) do
        if same_value == 0 then same_value = point end
        if same_value ~= point then is_all_same = false break end
    end
    return is_all_same, same_value
end

local function is_twosame(all_points)
    local is_two_same = false
    local same_value = 0
    local not_same_value = 0
    if not all_points or #all_points ~= 3 then return is_two_same, same_value end
    for i = 1, 3 do
        for j = 1, 3 do
            if i ~= j then
                if all_points[i] == all_points[j] then
                    is_two_same = true
                    same_value = all_points[i]
                    break
                end
            end
        end
        if is_two_same == true and same_value ~= 0 then break end
    end
    if is_two_same == true and same_value ~= 0 then
        for index, point in ipairs(all_points) do
            if same_value ~= point then not_same_value = point break end
        end
    end
    ---filelog.sys_error("------is_twosame------", is_two_same, same_value, not_same_value)
    return is_two_same, same_value, not_same_value
end

--获得游戏结果
function CardTool.get_gameresult(gameResult)

    --返回结果
    local result_list = {
        all_cards = {},
        prize_ids = {},
        all_sum = 0,
    }
    local get_dices = {}
    table.insert(get_dices, tonumber(gameResult.dice1))
    table.insert(get_dices, tonumber(gameResult.dice2))
    table.insert(get_dices, tonumber(gameResult.dice3))

    if #get_dices ~= 3 then
        return result_list
    end
    local alldicesum = 0
    for index, dice in ipairs(get_dices) do
        table.insert(result_list.all_cards, dice)
        alldicesum = alldicesum + dice
    end
    result_list.all_sum = alldicesum

    ----判断是否是围骰
    local is_all_same, same_value = is_allsame(result_list.all_cards)
    --filelog.sys_error("-------is_all_same, same_value----", is_all_same, same_value)
    if is_all_same == true and same_value ~= nil and same_value ~= 0 then
        table.insert(result_list.prize_ids, 2400 + same_value)
        table.insert(result_list.prize_ids, 2200 + same_value)
        table.insert(result_list.prize_ids, 2300 + same_value)
        table.insert(result_list.prize_ids, 2501)
    else
        if alldicesum >= EDICESumType.SUM_LITTLE[1] and alldicesum <= EDICESumType.SUM_LITTLE[2]  then
            table.insert(result_list.prize_ids, 1101)
        elseif alldicesum >= EDICESumType.SUM_BIG[1] and alldicesum <= EDICESumType.SUM_BIG[2] then
            table.insert(result_list.prize_ids, 1102)
        end
        local is_two_same, two_same_value, one_value = is_twosame(result_list.all_cards)
        --filelog.sys_error("-----is_two_same, two_same_value, one_value-----", is_two_same, two_same_value, one_value)
        if is_two_same == true and two_same_value ~= nil and two_same_value ~= 0 then
            table.insert(result_list.prize_ids, 2300 + two_same_value)
            table.insert(result_list.prize_ids, 2200 + two_same_value)
            table.insert(result_list.prize_ids, 2200 + one_value)
        else
            --filelog.sys_error("-----no allsame and no twosame------")
            for index, value in ipairs(result_list.all_cards) do
                table.insert(result_list.prize_ids, 2200 + value)
            end
        end
    end
    local bit_id = EDICEBtnIDSum[alldicesum]
    table.insert(result_list.prize_ids, bit_id)
    result_list.all_sum = alldicesum
    return result_list
end

return CardTool